package com.wuzf.utils;

import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

import com.wuzf.entity.User;

public class PasswordHelper {
	
	private String algorithmName = "MD5";
	
	private final int hashIterations = 2;
	
	public void encryptPassword(User user) {
		user.setSalt(new SecureRandomNumberGenerator().nextBytes().toHex());
		//加密
		String newPassword = getPassword(user);
		user.setPassword(newPassword);
	}
	
	public String getPassword(User user) {
		return new SimpleHash(algorithmName,
				user.getPassword(),
				ByteSource.Util.bytes(user.getCredentialsSalt()),
				hashIterations).toHex();
	}
}
